<?php
	
	class Durak extends CI_Controller
	{
		function __construct()
		{
			parent::__construct();
			$this->load->model('card_model');
			$this->load->helper('url');
			$this->load->library('session');
		}

		public function index()
		{
			$this->clear_data();
			redirect('durak/initialize', 'refresh');
		} //end index

		function clear_data()
		{
			$this->card_model->clear_data('deck');
			$this->card_model->clear_data('hand');
			$this->card_model->clear_data('field');
			$this->card_model->clear_data('trump');
		}

		function initialize()
		{
			$deck = $this->get_cards();
			foreach($deck as $cards):
				$this->set_deck($deck);
			endforeach;
			$hand1 = array();
			$hand2 = array();
			$this->set_hand(1, $hand1);
			$this->set_hand(2, $hand2);
			$this->set_trump();
			$hand1 = $this->get_hand(1);
			$hand2 = $this->get_hand(2);
			$player['first'] = $this->get_hand(1);
			$player['second'] = $this->get_hand(2);
			$player['table'] = $this->get_table();
			$player['trump'] = $this->card_model->get_trump();
			$this->load->view('durak/p1_attack_view', $player);
		}

//---------------------------------------------------------------------------------------------------------------------------------------//
// 															 SET FUNCTIONS                                                               //
//---------------------------------------------------------------------------------------------------------------------------------------//
		function set_deck($deck)
		{
			foreach($deck as $card):
				$this->card_model->set_deck($card['suit_id'], $card['face']);
			endforeach;
		}

		function set_hand($player, $hand)
		{
			$deck = $this->get_deck();
			while(count($hand) != 6) //push card from deck in hand
			{					     //if hand count is less than 6
				$card = array_pop($deck);
				array_push($hand, $card);
				$this->card_model->deal($card['suit_id'], $card['face']);	
			}

			foreach($hand as $cards): //all cards in hand to be put in database
				$this->card_model->set_hand($player, $cards['suit_id'], $cards['face']);
			endforeach;
		}

		function set_trump()
		{
			$deck = $this->get_deck();
			$trump = array_pop($deck);
			$this->card_model->set_trump($trump['suit_id'], $trump['face']);
			$this->card_model->deal($trump['suit_id'], $trump['face']);
		}

		function set_attack($player, $index) //this function checks whether attack
		{									 //is valid
			$hand = $this->get_hand($player);
			$table = $this->get_table();
			$values = array();
			if(count($table) == 0)
				$this->launch_attack($player, $index);
			else
			{
				foreach($table as $card)
				{
					array_push($values, $card['face']);
				}
				if(in_array($hand[$index]['face'], $values))
					$this->launch_attack($player, $index);
			}
		}

		function launch_attack($player, $index) //this function puts the cards on the table
		{									    //and removes them from players hand
			$hand = $this->get_hand($player);
			$card = $hand[$index];
			$this->card_model->remove_from($player, $card['suit_id'], $card['face']);
			$this->card_model->set_table($card['suit_id'], $card['face']);
			unset($card);
			$hand = array_values($hand);
			$data['table'] = $this->get_table();
			$data['first'] = $this->get_hand(1);
			$data['second'] = $this->get_hand(2);
			$data['trump'] = $this->get_trump();
			if($player == 1)
			{
				$this->load->view('durak/p2_defense_view', $data);
			}
			if($player == 2)
			{	
				$this->load->view('durak/p1_defense_view', $data);
			}
		}

		function set_defense($player, $index)
		{
			$hand = $this->get_hand($player);
			$table = $this->get_table();
			$card = $hand[$index];
			$attack = end($table);
			$attack['face'] = intval($attack['face']);
			$card['face'] = intval($card['face']);
			$trump = $this->get_trump();
			foreach($trump as $t):
				$suit = $t['suit_id'];
			endforeach;
			if($attack['suit_id'] == $card['suit_id'])
			{
				if($card['face'] > $attack['face'])
				{
					$this->launch_defense($player, $index);
				}
			}
			else //trump suit concept
			{
				if($card['suit_id'] == $suit)
				{
					$this->launch_defense($player, $index);
				}
			}
		}

		function launch_defense($player, $index)
		{
			$hand = $this->get_hand($player);
			$table = $this->get_table();
			$card = $hand[$index];
			$this->card_model->remove_from($player, $card['suit_id'], $card['face']);
			$this->card_model->set_table($card['suit_id'], $card['face']);
			unset($card);
			$hand = array_values($hand);
			$data['table'] = $this->get_table();
			$data['first'] = $this->get_hand(1);
			$data['second'] = $this->get_hand(2);
			$data['trump'] = $this->get_trump();
			if($player == 1)
			{
				$this->load->view('durak/p2_attack_view', $data);
			}
			if($player == 2)
			{
				$this->load->view('durak/p1_attack_view', $data);
			}
		}

		function remove_from($player, $index)
		{
			$hand = $this->get_hand($player);
			$selected = $hand[$index]; 
			$this->card_model->remove_from($player, $selected['suit_id'], $selected['face']);
			unset($selected);
			$hand = array_values($hand);
		}

		function set_end($player)
		{
			$this->card_model->empty_table();
			$hand1 = $this->get_hand(1);
			$hand2 = $this->get_hand(2);
			if(count($hand1) < 6)
			{
				$this->set_hand(1, $hand1);
			}
				
			if(count($hand2) < 6)
			{
				$this->set_hand(2, $hand2);
			}	
			$data['first'] = $this->get_hand(1);
			$data['second'] = $this->get_hand(2);
			$data['table'] = $this->get_table();
			$data['trump'] = $this->get_trump();
			if($player == 1)
			{
				$this->load->view('durak/p2_attack_view', $data);
			}
			if($player == 2)
			{
				$this->load->view('durak/p1_attack_view', $data);	
			}
		}

		function set_accept($player)
		{
			$table = $this->get_table();
			foreach($table as $card)
			{
				$this->card_model->set_hand($player, $card['suit_id'], $card['face']);
			}
			$this->card_model->empty_table();
			$data['first'] = $this->get_hand(1);
			$data['second'] = $this->get_hand(2);
			$data['table'] = $this->get_table();
			$data['trump'] = $this->get_trump();
			if($player == 1)
			{	
				$this->load->view('durak/p2_attack_view', $data);
			}
			if($player == 2)
			{
				$this->load->view('durak/p1_attack_view', $data);
			}
		}
//---------------------------------------------------------------------------------------------------------------------------------------//
// 															 GET FUNCTIONS                                                               //
//---------------------------------------------------------------------------------------------------------------------------------------//
		function get_cards() //initial set up for deck
		{
			$deck = $this->card_model->get_cards();
			shuffle($deck);
			return $deck;
		}

		function get_deck() //actual deck to be used and modified for the game
		{
			$deck = $this->card_model->get_deck();
			return $deck;
		}

		function get_hand($player)
		{
			$hand = $this->card_model->get_hand($player);
			return $hand;
		} //end get_hand

		function get_table()
		{
			$table = $this->card_model->get_table();
			return $table;
		}

		function get_trump()
		{
			$trump = $this->card_model->get_trump();
			return $trump;
		}
	}

?>